package com.ruoyi.project.shortUrl.config.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.framework.web.domain.BaseEntity;
import com.ruoyi.project.shortUrl.config.domain.ShortConfig;
import com.ruoyi.project.shortUrl.config.enums.ShortUrlOpenWay;
import com.ruoyi.project.shortUrl.config.mapper.ShortConfigMapper;

/**
 * 短链接系统设置对象 web_config
 * 
 * @author motisy
 * @date 2020-03-29
 */
@Service
public class ShortConfigService extends BaseEntity
{
	@Autowired
	private ShortConfigMapper mapper;
	
    private static final long serialVersionUID = 1L;

    /**
     * 短链接后加上-后缀，即可进入修改界面
     */
    public static final String EDIT_SHORT_URL_PATH_SUFFIX = "edit_short_url_path_suffix";
    
    /**
     * 目标地址打开方式，使用框架fram，还是转发forward
     */
    public static final String TARGET_URL_OPEN_WAY = "target_url_open_way";
    
    /**
     * 注册用户生成短链接的时效
     */
    public static final String REGISTER_DEFAULT_SHORT_EXPIRE_TIME = "register_default_short_expire_time";
    
    /**
     * 注册用户生成短链接数限制
     */
    public static final String REGISTER_DEFAULT_SHORT_LIMIT_SIZE = "register_default_short_limit_size";
    
    /**
     * 非注册用户生成短链接的时效
     */
    public static final String VISITOR_SHORT_EXPIRE_TIME = "visitor_short_expire_time";
    
    /**
     * 非注册用户生成短链接数限制
     */
    public static final String VISITOR_SHORT_LIMIT_SIZE = "visitor_short_limit_size";

    /**
     * 短链接后加上-后缀，即可进入修改界面
     * @return
     */
	public String getEDIT_SHORT_URL_PATH_SUFFIX() {
		return getConfig(EDIT_SHORT_URL_PATH_SUFFIX).getValue();
	}

	/**
	 * 目标地址打开方式，使用框架fram，还是转发forward
	 * @return
	 */
	public ShortUrlOpenWay getTARGET_URL_OPEN_WAY() {
		return ShortUrlOpenWay.getEnum(getConfig(TARGET_URL_OPEN_WAY).getValue());
	}

	/**
	 * 注册用户生成短链接的时效
	 * @return
	 */
	public int getREGISTER_DEFAULT_SHORT_EXPIRE_TIME() {
		int day = 30;
		try {
			day = Integer.parseInt(getConfig(REGISTER_DEFAULT_SHORT_EXPIRE_TIME).getValue());
		} catch (Exception e) {
			day = 30;
		}
		return day;
	}

	/**
	 * 注册用户生成短链接数限制
	 * @return
	 */
	public int getREGISTER_DEFAULT_SHORT_LIMIT_SIZE() {
		int size = 10;
		try {
			size = Integer.parseInt(getConfig(REGISTER_DEFAULT_SHORT_LIMIT_SIZE).getValue());
		} catch (Exception e) {
			size = 10;
		}
		return size;
	}

	/**
	 * 非注册用户生成短链接的时效
	 * @return
	 */
	public int getVISITOR_SHORT_EXPIRE_TIME() {
		int day = 30;
		try {
			day = Integer.parseInt(getConfig(VISITOR_SHORT_EXPIRE_TIME).getValue());
		} catch (Exception e) {
			day = 30;
		}
		return day;
	}

	/**
	 * 非注册用户生成短链接数限制
	 * @return
	 */
	public int getVISITOR_SHORT_LIMIT_SIZE() {
		return Integer.parseInt(getConfig(VISITOR_SHORT_LIMIT_SIZE).getValue());
	}
	
	public boolean getREGIST() {
		ShortConfig r = getConfig("regist");
		if(r==null) {
			return false;
		}else {
			Boolean b = Boolean.valueOf(r.getValue());
			return b;
		}
	}
	
	public ShortConfig getConfig(String key) {
		ShortConfig shortConfig = mapper.selectShortConfigById(key);
		return shortConfig;
	}
	
	public String getConfigValue(String key) {
		ShortConfig shortConfig = mapper.selectShortConfigById(key);
		return shortConfig==null?"":shortConfig.getValue();
	}
	
	public int updateConfig(String key, String value) {
		ShortConfig shortConfig = getConfig(key);
//		if(StringUtils.isNotBlank(value) && !value.equals(shortConfig.getValue())) {
			shortConfig.setValue(value);
			return mapper.updateShortConfig(shortConfig);
//		}
//		return 0;
	}

	public List<ShortConfig> getShortConfigList(ShortConfig filter) {
		return mapper.selectShortConfigList(filter);
	}
}
